<?php

declare(strict_types=1);

namespace app\model;

use think\model;

class Base extends model
{
    public static function getCurrentPk()
    {
        return (new static)->pk ?: "id";
    }

    // 排序修改
    public static function sorts($data)
    {
        try {
            $info = self::find($data['id']);
            if ($info->sort != $data['sort']) {
                $info->sort = $data['sort'];
                $info->save();
                return json(['code' => 0, 'msg' => '修改成功!']);
            }
        } catch (\Exception $e) {
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }

    // 状态修改 1,正常; 2,非正常
    public static function state(int $id)
    {
        try {
            $info         = self::find($id);
            $info->status = 1 - $info['status'];
            $info->save();
            return json(['code' => 0, 'msg' => '修改成功!', 'status'=>$info->status]);
        } catch (\Exception $e) {
            return json(['code' => 1, 'msg' => $e->getMessage()]);
        }
    }
}
